package com.neu.exception;

import com.neu.utils.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BusinessException.class) //代表该方法可以处理BusinessException类型的异常
    public ResultModel handlerBusinessException(BusinessException businessException){

        ResultModel resultModel = new ResultModel();

        resultModel.setCode(500);
        resultModel.setMsg(businessException.getMessage());

        return resultModel;
    }

    @ExceptionHandler(Exception.class) //代表该方法可以处理Exception类型的异常
    public ResultModel handlerException(Exception exception){

        ResultModel resultModel = new ResultModel();

        resultModel.setCode(500);

        logger.error(exception.getMessage());
        resultModel.setMsg("服务器繁忙,请稍后再试");

        return resultModel;
    }

}
